# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="byronhulcher"
__date__ ="$Mar 9, 2011 4:43:34 PM$"

"""
To create a server, you need to:

create a socket
bind the socket to an address and port
listen for incoming connections
wait for clients
accept a client
send and receive data
To create a client, you need to:

create a socket
connect to the server
send and receive data
"""

import socket
from socket import *


class Server:
    def __init__(self):
        self.myipstring = ''
        self.port = 50000
        print "I am ",self.myipstring, self.port
        backlog = 2
        self.size = 1024

        self.socket = socket(AF_INET, SOCK_STREAM)
        self.socket.bind((self.myipstring,self.port))
        self.socket.listen(backlog)

    def update(self):
        client, address = self.socket.accept()
        data = client.recv(self.size)
        if data:
            print "Received:", data, "from", address
            data = "hello I'm  your server %s" %gethostbyname(gethostname())
            client.send(data)
        client.close()

if __name__ == "__main__":
    myserver = Server()
    tick = 0
    while True:
        tick += 1
        print 'tick', tick
        myserver.update()


